package com.tanhua.server.service;


import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContractVo;
import com.tanhua.domain.vo.LikeVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.UserInfoVo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class IMService {

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private CommentApi commentApi;

    /**
     * 作用：根据环信的id号查询用户的信息
     * @param huanxinId
     * @return
     */
    public ResponseEntity getHuanxinUserInfo(Long huanxinId) {
        //1. 得到UserInfo对象
        UserInfo userInfo = userInfoApi.findById(huanxinId);
        //2. 得到UserInfoVo
        UserInfoVo userInfoVo = new UserInfoVo();
        if(userInfo!=null){
            BeanUtils.copyProperties(userInfo,userInfoVo);
            if(userInfo.getAge()!=null){
                userInfoVo.setAge(userInfo.getAge()+"");
            }
        }
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 添加好友
     * @param friendId
     * @return
     */
    public ResponseEntity addContact(Integer friendId) {
        //1. 添加好友
        Long userId = UserHolder.getUserId();
        friendApi.save(userId,friendId);

        //2.通知环信添加好友
        huanXinTemplate.contactUsers(userId,friendId.longValue());

        //3.返回响应
        return ResponseEntity.ok(null);
    }

    /**
     *接口名称：联系人列表
     *接口路径：GET/messages/contacts
     */
    public ResponseEntity queryContractList(Integer page, Integer pagesize, String keyword) {
        //1. 得到联系人的PageResult，里面存储的是Friend
        Long userId = UserHolder.getUserId();
        PageResult pageResult = friendApi.findFriendByUserId(userId,page,pagesize);

        //2.得到pageResult的items， list<Friend>
        List<Friend> friendList = (List<Friend>) pageResult.getItems();

        //3. 创建VoList
        List<ContractVo> voList = new ArrayList<>();

        //4. 把friendList内容转移到volist
        if(friendList!=null){
            for (Friend friend : friendList) {
                //查询朋友的用户信息
                UserInfo userInfo = userInfoApi.findById(friend.getFriendId());
                if(!StringUtils.isEmpty(keyword)) {
                    if (!userInfo.getNickname().contains(keyword)) {
                        continue;// 如果用户的昵称不包含搜索的关键字就直接跳过
                    }
                }
                ContractVo vo = new ContractVo();
                if(userInfo!=null){
                    BeanUtils.copyProperties(userInfo,vo);
                    vo.setUserId(userInfo.getId()+"");
                    vo.setCity(userInfo.getCity().split("-")[0]);
                }
                voList.add(vo);
            }
        }
        //5. 把数据封装到pageResult
        pageResult.setItems(voList);

        //返回数据
        return ResponseEntity.ok(pageResult);
    }

    //显示点赞、评论、喜欢列表业务
    public ResponseEntity<Object> getList(Integer page, Integer pagesize,Integer contentType) throws InvocationTargetException, IllegalAccessException {
        PageResult pageResult = commentApi.queryByUser(page,pagesize,UserHolder.getUserId(),contentType);
        List<Comment> items = (List<Comment>) pageResult.getItems();
        ArrayList<LikeVo> likeVos = new ArrayList<>();
        if(items != null){
            for (Comment item : items) {
                LikeVo likeVo = new LikeVo();
                UserInfo userInfo = userInfoApi.findById(item.getUserId());
                org.apache.commons.beanutils.BeanUtils.copyProperties(likeVo,userInfo);
                likeVo.setId(userInfo.getId().toString());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:DD");
                likeVo.setCreateDate(sdf.format(userInfo.getCreated()));
                likeVos.add(likeVo);
            }
        }
        pageResult.setItems(likeVos);
        return ResponseEntity.ok(pageResult);
    }
}
